package com.koron.common.core.business.workflow.util;

import com.alibaba.fastjson.JSON;
import com.koron.common.core.business.workflow.bean.param.StartParam;
import com.koron.common.core.business.workflow.bean.param.SubmitParam;
import com.koron.common.core.business.workflow.bean.properties.WorkFlowConfigProperties;
import com.koron.common.core.util.SpringUtils;
import lombok.SneakyThrows;
import org.apache.commons.beanutils.BeanUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author mwangli
 * @date 2021/1/27 15:41
 **/
public class WorkflowDemo {

    private static final String BASE_URL = SpringUtils.getBean(WorkFlowConfigProperties.class).getUrl();

    private static final String START_URL = BASE_URL + "/process/start.htm";

    private static final String SUBMIT_URL = BASE_URL + "/process/submit.htm";

    private static final String LIST_URL = BASE_URL + "/process/list.htm";

    private static final String VIEW_URL = BASE_URL + "/process/view.htm";


    public static String start(StartParam param) {
        return HttpUtil.sendPost(START_URL, JSON.toJSONString(param));
    }

    public static String start(String appCode, String setCode, String processCode, String billId, String operator, Map<String, String> formVariables) {
        StartParam param = new StartParam();
        param.setSecret(SecretUtil.getSecret(appCode));
        param.setSetCode(setCode);
        param.setProcessCode(processCode);
        param.setBillId(billId);
        param.setOperator(operator);
        param.setFormVariables(formVariables);
        param.setTitle("EAM");
        return start(param);
    }

    public static String submit(SubmitParam param) {
        return HttpUtil.sendPost(SUBMIT_URL, JSON.toJSONString(param));
    }

    public static String submit(String appCode, String setCode, String processCode, String billId, String operator, String taskId, Integer operation, Map<String, String> formVariables) {
        SubmitParam param = new SubmitParam();
        param.setSecret(SecretUtil.getSecret(appCode));
        param.setSetCode(setCode);
        param.setProcessCode(processCode);
        param.setBillId(billId);
        param.setOperator(operator);
        param.setTaskId(taskId);
        param.setOperation(operation);
        param.setFormVariables(new HashMap<String,String>());
//        param.setRejectNodeCode("draft-Process-task0");
        return submit(param);
    }

    @SneakyThrows
    public static String list(SubmitParam param) {
        return HttpUtil.sendGet(LIST_URL, BeanUtils.describe(param));
    }

    @SneakyThrows
    public static String list(String appCode, String setCode, String processCode, String operator) {
        HashMap<String, String> map = new HashMap<>();
        map.put("secret", SecretUtil.getSecret(appCode));
        map.put("setCode", setCode);
        map.put("processCode", processCode);
        map.put("operator", operator);
        map.put("type", "2");
        return HttpUtil.sendGet(LIST_URL, map);
    }

    @SneakyThrows
    public static String view(String appCode, String setCode, String processCode, String operator, String billId) {
        HashMap<String, String> map = new HashMap<>();
        map.put("secret", SecretUtil.getSecret(appCode));
        map.put("setCode", setCode);
        map.put("processCode", processCode);
        map.put("operator", operator);
        map.put("billId", billId);
        return HttpUtil.sendGet(VIEW_URL, map);
    }
}
